import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'couponPoolQueryM',

  state: {
    selectTag: [],
    selectName: [],
    selectBd: undefined,
    selectUser: [],
    selectCompany: [],
    selsecInpool: [],
    tableData: [],
    selectCol: [],
    selectPoolName: [],
    selectPoolTag: [],
    comCode: [],
  },

  effects: {
    //如用户池操作
    *addPool({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.InPool, payload);
      if (response.code == 0) {
        message.success('入池成功');
        callback(true);
      } else {
        message.error(`${response.msg}`);
      }
    },

    //出用户池
    *deletePool({ payload }, { call, put }) {
      const response = yield call(api.poolQuery.outPool, payload);

      return response;
    },

    //入用户池下标签
    *searchPoolTag({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.searchPoolTag, payload);

      if (response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            selectPoolTag: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //入用户池下的券池名称
    *searchPoolName({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.searchPoolName, payload);

      if (response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            selectPoolName: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //券池类型资产类别下拉查询
    *findName({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.findName, payload);
      if (response?.code == 0) {
        yield put({
          type: 'save',
          payload: {
            selectName: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取标的
    *findBd({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.findBd, payload);

      if (response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            selectBd: response.data.dataSource,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取标签
    *findTags({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.findTags, payload);

      if (response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            selectTag: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取入池人员
    *findUser({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.findUser, payload);

      if (response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            selectUser: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取发行主体
    *findCompany({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.findCompany, payload);

      if (response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            selectCompany: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //查询所有数据
    *findAll({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.findALL, payload);

      if (response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //资管查询常规库、私募产品库、高息债券库信息
    *findSuperData({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.findSuperData, payload);
      if (response && response.code == 0) {
        callback(response);
      } else {
        callback();
        message.error(response?.msg || '查询失败');
      }
    },

    //修改投资额度
    *saveInvestmentAmounts({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.saveInvestmentAmounts, payload);
      if (response && response.code == 0) {
        callback(true);
      } else {
        callback();
        message.error(response?.msg || '保存失败');
      }
    },
    //修改内部评级
    *saveInternalRating({ payload, callback }, { call, put }) {
      const response = yield call(api.poolQuery.saveInternalRating, payload);
      if (response && response.code == 0) {
        callback(true);
      } else {
        callback();
        message.error(response?.mesg || '保存失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
